package com.shop.expandablelist;

import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * date:2021/12/23
 * author:wsm(admin)
 * funcation:
 */
public class CartAdapter extends BaseExpandableListAdapter {
    private List<DataBean> dataBeanList;

    public CartAdapter(List<DataBean> dataBeanList) {
        this.dataBeanList = dataBeanList;
    }

    @Override
    public int getGroupCount() {
        return dataBeanList == null ? 0 : dataBeanList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return dataBeanList.get(groupPosition).getList() == null ? 0 : dataBeanList.get(groupPosition).getList().size();
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        DataBean dataBean = dataBeanList.get(groupPosition);
        ViewHolder1 holder1;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.cart_recy_one, null);
            holder1 = new ViewHolder1(convertView);
            convertView.setTag(holder1);
        } else {
            holder1 = (ViewHolder1) convertView.getTag();
        }
        //获取商家的名称
        holder1.sJname.setText(dataBean.getSellerName());

        //判断当前商家是否是选中状态
        boolean allSjShopselected = isAllSjShopselected(groupPosition);
        holder1.sJcb.setChecked(allSjShopselected);

        //设置点击复选框
        holder1.sJcb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnCartListener != null) {
                    mOnCartListener.onCartSjChecked(groupPosition);
                }
            }
        });

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        List<DataBean.ListBean> list = dataBeanList.get(groupPosition).getList();
        DataBean.ListBean listBean = list.get(childPosition);
        ViewHolder2 holder2;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.cart_recy_two, null);
            holder2 = new ViewHolder2(convertView);
            convertView.setTag(holder2);
        } else {
            holder2 = (ViewHolder2) convertView.getTag();
        }
        //设置图片
//        Uri uri = Uri.parse(listBean.getImages());
//        holder2.sdvImage.setImageURI(uri);
        holder2.sdvImage.setImageResource(R.mipmap.icon_shop);

        //设置商品名
        holder2.shopname.setText(listBean.getTitle());

        //设置价格
        holder2.txtPrice.setText("￥：" + listBean.getPrice());

        //加减器
        holder2.subView.setNumber(listBean.getNum());


        //设置加减器是否选中
        holder2.shopcb.setChecked(listBean.getSelected() == 1);
        holder2.shopcb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnCartListener != null) {
                    mOnCartListener.onCartShopChecked(groupPosition, childPosition);
                }
            }
        });

        //加减器通过接口回调暴露给外面
        holder2.subView.setOnNumberChangeListener(new MyAddSubView.OnNumberChangeListener() {
            @Override
            public void onNumberChange(int num) {
                if (mOnCartListener != null) {
                    mOnCartListener.onCartSubviewChecked(groupPosition, childPosition, num);
                }
            }
        });
        return convertView;
    }


    //组的数据
    public static class ViewHolder1 {

        private final CheckBox sJcb;
        private final TextView sJname;

        public ViewHolder1(View rootView) {
            sJcb = rootView.findViewById(R.id.cb_sj);
            sJname = rootView.findViewById(R.id.txt_sj_name);
        }

    }

    //组的数据
    public static class ViewHolder2 {


        private final CheckBox shopcb;
        private final ImageView sdvImage;
        private final TextView shopname;
        private final TextView txtPrice;
        private final MyAddSubView subView;

        public ViewHolder2(View rootView) {
            shopcb = rootView.findViewById(R.id.cb_shop);
            sdvImage = rootView.findViewById(R.id.sdv_image);
            shopname = rootView.findViewById(R.id.txt_shopname);
            txtPrice = rootView.findViewById(R.id.txt_shopprice);
            subView = rootView.findViewById(R.id.subview);
        }

    }

    /**
     * 判断商家的商品是否选中
     *
     * @param groupPosition
     * @return
     */
    public boolean isAllSjShopselected(int groupPosition) {
        //获取商家数据
        DataBean dataBean = dataBeanList.get(groupPosition);
        //获取商品数据
        List<DataBean.ListBean> list = dataBean.getList();

        for (DataBean.ListBean listBean : list) {
            //判断商品是否都选中，一个没选中商家的复选框都不选中 ==0是选中状态
            if (listBean.getSelected() == 0) {
                return false;
            }
        }
        return true;
    }


    /**
     * 底部全选框的状态
     *
     * @return
     */
    public boolean isBottomAllselected() {
        for (int i = 0; i < dataBeanList.size(); i++) {
            DataBean dataBean = dataBeanList.get(i);
            List<DataBean.ListBean> list = dataBean.getList();
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getSelected() == 0) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 计算商品总的数量
     *
     * @return
     */
    public int ShopNumber() {
        int shopnums = 0;
        for (int i = 0; i < dataBeanList.size(); i++) {
            DataBean dataBean = dataBeanList.get(i);
            List<DataBean.ListBean> list = dataBean.getList();
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getSelected() == 1) {
                    int num = list.get(j).getNum();
                    shopnums += num;
                }
            }
        }
        return shopnums;
    }


    /**
     * 计算商品的总价格
     *
     * @return
     */
    public float ShopPrices() {
        float prices = 0;
        for (int i = 0; i < dataBeanList.size(); i++) {
            DataBean dataBean = dataBeanList.get(i);
            List<DataBean.ListBean> list = dataBean.getList();
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getSelected() == 1) {
                    int price = list.get(j).getPrice();
                    int num = list.get(j).getNum();
                    prices += price * num;

                }
            }
        }
        return prices;
    }


    /**
     * 商家选中时商品都选中
     *
     * @param groupPosition
     * @param isSelected
     */
    public void SjSelected(int groupPosition, boolean isSelected) {
        DataBean dataBean = dataBeanList.get(groupPosition);
        List<DataBean.ListBean> list = dataBean.getList();
        for (int i = 0; i < list.size(); i++) {
            DataBean.ListBean listBean = list.get(i);
            listBean.setSelected(isSelected ? 1 : 0);
        }
    }

    /**
     * 当商品都选中时更新商家的状态
     *
     * @param groupPosition
     * @param childPosition
     */
    public void ShopSelected(int groupPosition, int childPosition) {
        DataBean dataBean = dataBeanList.get(groupPosition);
        List<DataBean.ListBean> list = dataBean.getList();
        DataBean.ListBean listBean = list.get(childPosition);
        listBean.setSelected(listBean.getSelected() == 0 ? 1 : 0);
    }

    /**
     * 底部全选框状态选中时更新以上所有商品状态
     *
     * @param selected
     */
    public void ButtomSelected(boolean selected) {
        for (int i = 0; i < dataBeanList.size(); i++) {
            DataBean dataBean = dataBeanList.get(i);
            List<DataBean.ListBean> list = dataBean.getList();
            for (int j = 0; j < list.size(); j++) {
                list.get(j).setSelected(selected ? 1 : 0);
            }
        }
    }

    /**
     * 当商品加减器被点击时改变里面商品的数量
     *
     * @param groupPosition
     * @param childPosition
     * @param number
     */
    public void subviewSelected(int groupPosition, int childPosition, int number) {
        DataBean dataBean = dataBeanList.get(groupPosition);
        List<DataBean.ListBean> list = dataBean.getList();
        DataBean.ListBean listBean = list.get(childPosition);
        listBean.setNum(number);
    }


    /**
     * 接口回调
     */
    public interface OnCartListener {
        /**
         * 点击商家复选框的时候调用
         *
         * @param groupPosition
         */
        void onCartSjChecked(int groupPosition);

        /**
         * 点击商品复选框的时候调用
         *
         * @param groupPosition
         * @param childPosition
         */
        void onCartShopChecked(int groupPosition, int childPosition);

        /**
         * 点击购物车加减器的时候调用
         *
         * @param groupPosition
         * @param childPosition
         * @param number
         */
        void onCartSubviewChecked(int groupPosition, int childPosition, int number);
    }


    public OnCartListener mOnCartListener;

    /**
     * 给商家商品加减器设置监听
     *
     * @param onCartListener
     */
    public void setOnCartListener(OnCartListener onCartListener) {
        mOnCartListener = onCartListener;
    }


    //下面的东西不用写
    @Override
    public Object getGroup(int groupPosition) {
        return null;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return 0;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return 0;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }
}
